<template>
  <div class="app-container">
    <el-form  class="query-wrapper" :model="queryParams"  :inline="true">
      <el-form-item  prop="carrierName">
        <el-input v-model="queryParams.carrierName" clearable style="width: 180px" placeholder="Carrier Name" />
      </el-form-item>
      <el-form-item>  
        <el-button type="primary" icon="search" @click="search">{{ $t("ship.button.search") }}</el-button>
        <SearchReset :query-params="queryParams" @reset="resetQuery"></SearchReset>
        <el-button type="primary" plain icon="Plus" @click="addClickEvent" v-hasPermi="['carrier:add']">
          {{ $t("ship.button.plus") }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>

  <el-table stripe class="daf-table" :data="listData" style="width: 100%" @sort-change="handleSortChange">  
    <el-table-column prop="carrierName" label="Carrier Name" sortable />
    <el-table-column prop="carrierCode" label="Carrier Code" sortable />
    <el-table-column prop="createBy" label="Created By" sortable />
    <el-table-column label="Operation" v-hasPermi="['carrier:edit']">
      <template #default="scope">
        <span class="btn-primary"  @click="editClickEvent(scope.row)">{{ $t("ship.button.edit") }}</span
        >&nbsp;&nbsp;
        <span class="btn-primary"   @click="removeClickEvent(scope.row.id)">{{ $t("ship.button.remove") }}</span>
        &nbsp;&nbsp;
        <span class="btn-primary"  @click="bankInfoClickEvent(scope.row.id)">{{
          $t("ship.button.bankInfo")
        }}</span>
      </template>
    </el-table-column>
  </el-table>

  <pagination
    v-show="total > 0"
    :total="total"
    :page="queryParams.pageNum"
    :limit="queryParams.pageSize"
    @pagination="pageQuery"
    style="float: right"
  />

  <el-dialog :width="600" destroy-on-close draggable :title="dialogTitle" v-model="dialogFormVisible">
    <el-form :model="formParam" :rules="rules" ref="custForm">
      <el-row>
        <el-col :span="24">
          <el-form-item label="Carrier Name" label-width="120px" prop="carrierName" required>
            <el-input v-model="formParam.carrierName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="Carrier Code" label-width="120px" prop="carrierCode">
            <el-input v-model="formParam.carrierCode"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t("ship.button.cancel") }}</el-button>
        <el-button @click="resetForm" type="warning">{{ $t("ship.button.reset") }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t("ship.button.submit") }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup name="carrier">
import { ref, onMounted, reactive } from "vue"; 
import { ElNotification,ElMessageBox } from "element-plus";
import service from "@/utils/request";
const router = useRouter();
const custForm = ref(null);
const dialogTitle = ref("");
const initForm = {
  id: null,
  carrierName: null,
  carrierCode: null
};
const formParam = reactive(Object.assign({}, initForm));

const total = ref(0);

const rules = ref({
  deptId: [{ required: true, message: "department can't be blank" }]
});

const queryParams = reactive({
  pageNum: 1,
  pageSize: 20,
  carrierName: null,
  orderByColumn: undefined,
  isAsc: undefined
});

let recordData = ref([] as any);
const listData = ref([]);
const departments = ref([]);
const refreshTable = async () => {
  let result = await service.get("/shipping/carrier/getTable", { params: queryParams });
  listData.value = result.data;
  total.value = result.total;
};
const resetQuery = (data) => {
    Object.assign(queryParams, data)
    refreshTable()
}
const dialogFormVisible = ref(false);

const editClickEvent = (row: any) => {
  dialogTitle.value = "Update Carrier";
  dialogFormVisible.value = true;
  Object.keys(initForm).forEach((k) => (formParam[k] = row[k]));
  isAdd = false;
};

let isAdd = false;

const addClickEvent = () => {
  dialogTitle.value = "Add Carrier";
  isAdd = true;
  dialogFormVisible.value = true;
  Object.assign(formParam, initForm);
};

const resetForm = () => {
  // @ts-ignore
  custForm.value.resetFields();
};

const submitForm = () => {
  // @ts-ignore
  custForm.value.validate(async valid => {
    if (!valid) {
      // @ts-ignore
      ElNotification.warning({ title: "Prompt information", message: "verification not success！", position: "bottom-right" });
      return;
    }

    console.log(formParam);
    await service[isAdd ? "post" : "put"]("shipping/carrier", formParam);
    // @ts-ignore
    ElNotification.success({ title: "Prompt information", message: "save successful!", position: "bottom-right" });
    dialogFormVisible.value = false;
    refreshTable();
  });
};

const pageQuery = async e => {
  queryParams.pageNum = e.page;
  queryParams.pageSize = e.limit;
  refreshTable();
};

const search = () => {
  refreshTable();
};

const sleep = ms => {
  return new Promise(resolve => setTimeout(resolve, ms));
};

const handleSortChange = async (column: any) => {
  queryParams.orderByColumn = column.prop;
  queryParams.isAsc = column.order; //动态取值排序顺序
  refreshTable();
};

const removeClickEvent = id => {
  ElMessageBox.confirm("Confirm to delete the data？", "Confirmation Information", {
    type: "warning",
    confirmButtonText: "Confirm",
    cancelButtonText: "Cancel"
  }).then(async () => {
    await service.delete(`shipping/carrier/${id}`);
    refreshTable();
    // @ts-ignore
    ElNotification.success({ title: "Prompt information", message: "Delete successfully!", position: "bottom-right" });
  });
};


const  bankInfoClickEvent = (id)=>{
  router.push({path: "/basicData/bankInfo",query:{id:id,type:1}})
}

onMounted(async () => {
  refreshTable();
});
</script>
<style lang="scss" scoped>
    ::v-deep.el-table td.el-table__cell div{
    text-align: center;
  }
.el-table__expand-column .cell {
  display: none;
}
.c-expand {
  color: #888;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
  height: 100%;
}
.c-expand i {
  font-size: 16px;
}
.c-expand i.expand {
  font-size: 16px;
  transform: rotate(90deg);
  transition: all 0.25s;
}
</style>
